/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Author: Huawei OS Kernel Lab
 * Create: Thu Aug 15 16:14:23 2023
 */

#ifndef __LIBLINUX_ASM_MEMORY_H
#define __LIBLINUX_ASM_MEMORY_H

#include_next <asm/memory.h>

#ifdef CONFIG_ARM
#define VA_BITS 32
#define STRUCT_PAGE_MAX_SHIFT 6
#define VMEMMAP_SIZE  (UL(1) << (VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT))
#define VMEMMAP_START (PAGE_OFFSET - VMEMMAP_SIZE)
#endif

#ifdef CONFIG_ARM64
#undef  PAGE_OFFSET
#define PAGE_OFFSET (UL(1) << (VA_BITS - 1))

#undef VMEMMAP_SIZE
#define VMEMMAP_SIZE (UL(1) << (VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT))
#undef VMEMMAP_START
#define VMEMMAP_START	(PAGE_OFFSET - VMEMMAP_SIZE)

#ifdef CONFIG_KASAN
#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
#endif

#ifndef __ASSEMBLY__

#undef  __virt_to_phys
#define __virt_to_phys(x) ({						\
	phys_addr_t __x = (phys_addr_t)(x);				\
	__x & BIT(VA_BITS - 1) ? (__x & (PAGE_OFFSET-1)) + PHYS_OFFSET :	\
				 (__x - kimage_voffset); })


#undef  __phys_to_virt
#define __phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET) + PAGE_OFFSET)

#undef  virt_to_phys
#define virt_to_phys _virt_to_phys
static inline phys_addr_t _virt_to_phys(const volatile void *x)
{
	return __virt_to_phys((unsigned long)(x));
}

#undef  phys_to_virt
#define phys_to_virt _phys_to_virt
static inline void *_phys_to_virt(phys_addr_t x)
{
	return (void *)(__phys_to_virt(x));
}
#endif
#endif	/* __ASSEMBLY__ */

#endif /* __LIBLINUX_ASM_MEMORY_H */
